package com.atguigu.examsystemserver.controller;

import com.atguigu.examsystemserver.common.Result;
import com.atguigu.examsystemserver.entity.Notice;
import com.atguigu.examsystemserver.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notices")
@Tag(name = "公告管理", description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping("/list")
    @Operation(summary = "获取所有公告")
    public Result<List<Notice>> allList() {
        List<Notice> list = noticeService.list();
        return Result.success(list, "获取所有公告成功");
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新公告")
    public Result<List<Notice>> getNewNotice(@RequestParam(defaultValue = "3") Integer limit) {
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noticeLambdaQueryWrapper.orderByDesc(Notice::getCreateTime);
        Page<Notice> page = new Page<>(1, limit);
        noticeService.page(page, noticeLambdaQueryWrapper);
        return Result.success(page.getRecords(), "获取最新公告成功");
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID获取公告")
    public Result<Notice> getById(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        return Result.success(notice, "根据ID获取公告成功");
    }

    @GetMapping("/active")
    @Operation(summary = "获取启用的公告")
    public Result<List<Notice>> getActive() {
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noticeLambdaQueryWrapper.eq(Notice::getIsActive, true);
        List<Notice> list = noticeService.list(noticeLambdaQueryWrapper);
        return Result.success(list, "获取启用的公告成功");
    }

    @PostMapping("/add")
    @Operation(summary = "发布新公告")
    public Result<Notice> save(@RequestBody Notice notice) {
        boolean save = noticeService.save(notice);
        if (save) {
            return Result.success("发布新公告成功");
        } else {
            return Result.error("发布新公告失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除公告")
    public Result<Notice> remove(@PathVariable Integer id) {
        boolean b = noticeService.removeById(id);
        if (b) {
            return Result.success("删除公告成功");
        } else {
            return Result.error("删除公告失败");
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新公告")
    public Result<Notice> update(@RequestBody Notice notice) {
        boolean b = noticeService.updateById(notice);
        if (b) {
            return Result.success("更新公告成功");
        } else {
            return Result.error("更新公告失败");
        }
    }

    @PutMapping("/toggle/{id}")
    @Operation(summary = "切换公告状态")
    public Result activeUpdate(@RequestParam Boolean isActive,@PathVariable Integer id){
        LambdaUpdateWrapper<Notice> noticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        noticeLambdaUpdateWrapper.set(Notice::getIsActive,isActive).eq(Notice::getId,id);
        boolean remove = noticeService.update(noticeLambdaUpdateWrapper);
        if (remove){
            return Result.success("切换公告状态成功");
        }else {
            return Result.error("切换公告状态失败");
        }
    }
}
